﻿@{
    Layout = "~/Views/Shared/_UserHomeLayout.cshtml";
    var isEditMode = (bool)ViewBag.IsEditMode;
}
@using Nutshell.Blog.Common
@model Nutshell.Blog.Model.Article

@section style{
    <link href="~/Content/kindeditor/themes/default/default.css" rel="stylesheet" />
    <link href="~/Content/kindeditor/plugins/code/prettify.css" rel="stylesheet" />
    <style>
        .subtitle {
            border-bottom: 1px solid #f0f0f0;
            color: #999;
            padding: 8px 4px 2px 0px;
            margin-bottom: 5px;
            font-weight: bold;
        }

        .ctrl {
            margin-bottom: 5px;
        }

            .ctrl input[type=text] {
                border: solid 1px #ccc;
                padding-left: 4px;
                width: 560px;
                height: 25px;
            }

        .ctrl-group .btn {
            cursor: pointer;
            border: none;
            padding: 5px 13px;
            border-radius: 3px;
            margin-right: 10px;
        }

        .ctrl-group .btn_primary {
            color: #fff;
            background: #185da3;
        }

        .ctrl-group .btn_danger {
            color: #fff;
            background: #9b0909;
        }

        .ctrl-group {
            padding: 8px 0px;
        }

            .ctrl-group label {
                color: #999;
                font-weight: bold;
            }

            .ctrl-group select {
                margin-right: 40px;
                border: solid 1px #ccc;
                border-radius: 2px;
            }
    </style>
}


<div class="tabs">
    <ul class="tabs-nav">
        <li class="active"><a href="javascript:void(0);">编辑文章</a></li>
        <li><a href="/user/blog#list">文章列表</a></li>
        <li><a href="/user/blog#category">类别管理</a></li>
        <li><a href="/user/blog#draft">草稿箱</a></li>
        <li><a href="/user/blog#deleted">回收站</a></li>
    </ul>
    <div class="tabs-content">
        <div class="tab-panel active">
            <div class="container">
                <form>
                    <p class="subtitle">
                        文章标题
                    </p>
                    <div class="ctrl">
                        <input type="text" name="articletitle" class="" value="@(Model?.Title)" id="articletitle" placeholder="输入文章标题">
                    </div>
                    <div class="ctrl-group">
                        <label for="articlecolumn">我的分类:</label>
                        <select name="articlecolumn" id="articlecolumn" style="width:180px">
                            @{
                                var categories = ViewBag.CustomCategories as List<Nutshell.Blog.Model.CustomCategory>;
                                if (categories != null)
                                {
                                    foreach (var category in categories)
                                    {
                                        <option value="@category.Id">@category.CategoryName</option>
                                    }
                                }
                            }
                        </select>
                        <label for="articletype">系统分类:</label>
                        <select class="" id="articletype" style="width:180px">
                            @{
                                var syscategories = ViewBag.SystemCategories as List<Nutshell.Blog.Model.Article_Category>;
                                if (syscategories != null)
                                {
                                    foreach (var category in syscategories)
                                    {
                                        <option value="@category.Cate_Id">@category.Cate_Name</option>
                                    }
                                }
                            }
                        </select>
                    </div>
                    <p class="subtitle">
                        文章内容
                    </p>
                    <div class="am-form-group">
                        <textarea name="editor_body" id="editor_body" style="width:100%;height:500px;">
                        @Html.Raw(HttpUtility.HtmlDecode(Model?.Body))
                        </textarea>
                    </div>

                    <div class="ctrl-group">
                        @if (isEditMode)
                        {
                            <a href="#" id="btnSubmit" class="btn_primary btn">
                                <i class="am-icon-save"></i>
                                保存修改
                            </a>
                            if (Model.State == (int)ArticleStateEnum.NotPass)
                            {
                                <a href="#" id="btnPublish" class="btn_primary btn">
                                    <i class="am-icon-save"></i>
                                    提交审核
                                </a>
                            }
                        }
                        else
                        {
                            <a href="#" id="btnSubmit" class="btn_primary btn">
                                <i class="am-icon-save"></i>
                                保存并提交审核
                            </a>
                            <a href="#" class="btn_primary btn" id="btnSaveDraft">
                                <i class="am-icon-save"></i>
                                保存草稿
                            </a>
                        }
                        <a class="btn_danger btn" href="#" onclick="cancel(); return false;">
                            <i class="am-icon-close"></i>
                            取消
                        </a>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
@section script{
    <script src="~/Content/kindeditor/kindeditor-all-min.js"></script>
    <script src="~/Content/kindeditor/lang/zh-CN.js"></script>
    <script src="~/Content/kindeditor/plugins/code/prettify.js"></script>
    <script>
        $(function () {
            var editor_body = null;
            KindEditor.ready(function (K) {
                editor_body = K.create("#editor_body", {
                    @*@if (isEditMode) {
                        @("designMode: false,")
                     }*@
                    resizeType: 1,
                    allowPreviewEmoticons: false,
                    allowImageUpload: true,
                    uploadJson: "/article/uploadimage",
                    items: [
                        'source', 'fontname', 'fontsize', '|', 'forecolor', 'hilitecolor', 'bold', 'italic', 'underline',
                        'removeformat', '|', 'justifyleft', 'justifycenter', 'justifyright', 'insertorderedlist',
                        'insertunorderedlist', '|', 'code', 'emoticons', 'image', 'link']
                })
            });

            var $title = $("#articletitle");
            var $customCate = $("#articlecolumn");
            var $sysCategory = $("#articletype");

            $customCate.val(@(Model?.CustomCategory_Id));
            $sysCategory.val(@(Model?.SystemCategory_Id));


            $("#btnSubmit").click(function () {
                var title = $title.val();
                var body = editor_body.html();
                var customCate = $customCate.val();
                var sysCate = $sysCategory.val();
                if (title.trim().length > 0 && body.length > 0) {
                    $.post("/article/postArticle", { Title: title, Body: body, Content: editor_body.text(), CustomCategory_Id: customCate, SystemCategory_Id: sysCate, isEdit: '@isEditMode', articleid:'@(Model?.Article_Id)' }, function (data) {
                        console.log(data);
                        if (data.code == 0) {
                            layer.msg(data.msg, { icon: 6 });
                        } else {
                            layer.msg(data.msg, { icon: 5 });
                        }
                    })
                };
                return false;
            });
            $("#btnPublish").click(function () {
                $.post('/article/publish', { id:@(Model?.Article_Id)}, function (data) {
                    if (data.code == 0) {
                        layer.msg(data.msg, { icon: 6 });
                    } else {
                        layer.msg(data.msg, { icon: 5 });
                    }
                });
            });
            $("#btnSaveDraft").click(function () {
                var title = $title.val();
                var body = editor_body.html();
                var customCate = $customCate.val();
                var sysCate = $sysCategory.val();
                if (title.trim().length > 0 && body.length > 0) {
                    $.post("/article/postDraft", { Title: title, Body: body, Content: editor_body.text(), CustomCategory_Id: customCate, SystemCategory_Id: sysCate}, function (data) {
                        console.log(data);
                        if (data.code == 0) {
                            //layer.alert('提交成功，点击确认关闭窗口。', { icon: 6 }, function () {
                            //    CloseWebPage();
                            //});
                            layer.msg(data.msg, { icon: 6 });
                        } else {
                            layer.msg(data.msg, { icon: 5 });
                        }
                    })
                };
                return false;
            })
        });

        function cancel() {
            history.go(-1)
        }

        function CloseWebPage() {
            if (navigator.userAgent.indexOf("MSIE") > 0) {
                if (navigator.userAgent.indexOf("MSIE 6.0") > 0) {
                    window.opener = null;
                    window.close();
                } else {
                    window.open('', '_top');
                    window.top.close();
                }
            }
            else if (navigator.userAgent.indexOf("Firefox") > 0) {
                window.location.href = 'about:blank ';
            } else {
                window.opener = null;
                window.open('', '_self', '');
                window.close();
            }
        }
    </script>
}